package com.sgb.kjwl.viewmodel.enterprise;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.enterprise.BaseServiceDeatilEntity;
import com.sgb.kjwl.model.entity.enterprise.EpQuoteEntity;
import com.sgb.kjwl.model.entity.enterprise.ServiceNodeListEntity;
import com.sgb.kjwl.model.entity.enterprise.ServiceProjectClassEntity;
import com.sgb.kjwl.model.entity.enterprise.TransactionCasesEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PublishServiceDetailActivityModel extends AndroidViewModel {
    public MutableLiveData<List<ServiceNodeListEntity>> serviceNodeList = new MutableLiveData<>();
    public MutableLiveData<EpQuoteEntity> ldServiceDetail = new MutableLiveData<>();
    public MutableLiveData<List<TransactionCasesEntity>> ldCaseList = new MutableLiveData<>();
    public MutableLiveData<EpQuoteEntity> ldServiceStatus = new MutableLiveData<>();
    public MutableLiveData<String> ldServiceDelete = new MutableLiveData<>();
    public List<ServiceProjectClassEntity> serviceProList = new ArrayList<>();
    public MutableLiveData<Integer> ShowLoading = new MutableLiveData<>();
    public MutableLiveData<String> toast = new MutableLiveData<>();

    public PublishServiceDetailActivityModel(@NonNull Application application) {
        super(application);
    }


    /**
     * 获取发布服务详情
     */
    public void getServiceHallDetail(Context context, long id) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);

//        Map<String, Object> map = new HashMap();
//        map.put("id", id);
        NewAPIManager.getInstance().getEnterPrisenewAPI().serviceHallDetail(id).enqueue(new Callback<BaseEntity<BaseServiceDeatilEntity<EpQuoteEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<BaseServiceDeatilEntity<EpQuoteEntity>>> call, Response<BaseEntity<BaseServiceDeatilEntity<EpQuoteEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ldServiceDetail.postValue(response.body().getData().getServiceHall());
                } else {
                    MToast.showToast(context, response.body().getMsg());

                }
            }

            @Override
            public void onFailure(Call<BaseEntity<BaseServiceDeatilEntity<EpQuoteEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 获取案例列表
     */
    public void getTransactionList(Context context, String id) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getTransactionList(id, 20, 1).enqueue(new Callback<BaseEntity<ListResponse<List<TransactionCasesEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<TransactionCasesEntity>>>> call, Response<BaseEntity<ListResponse<List<TransactionCasesEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ldCaseList.postValue(response.body().getData().getList());
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<TransactionCasesEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 上下架
     */
    public void getPublishServiceEnable(Context context, long id, String status) {
//        Map<String, Object> map = new HashMap<>();
//        map.put("id", id);
//        map.put("status", status);
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);

        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getserviceEnable(id, status).enqueue(new Callback<BaseEntity<EpQuoteEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<EpQuoteEntity>> call, Response<BaseEntity<EpQuoteEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ldServiceStatus.postValue(response.body().getData());
                    toast.postValue(response.body().getMsg());
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<EpQuoteEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 删除服务
     */
    public void deleteService(Context context, long id) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().deleteService(id).enqueue(new Callback<BaseEntity<String>>() {
            @Override
            public void onResponse(Call<BaseEntity<String>> call, Response<BaseEntity<String>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ldServiceDelete.postValue(response.body().getData());
                } else {
                    MToast.showToast(context, response.body().getMsg());

                }
            }

            @Override
            public void onFailure(Call<BaseEntity<String>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 服务流程节点 列表
     *
     * @param serviceId
     */
    public void getServiceNodeList(String serviceId) {
        ShowLoading.postValue(1);
        NewAPIManager.getInstance().getEnterPrisenewAPI().getServiceNodeList(2, 1, serviceId).enqueue(new Callback<BaseEntity<ListResponse<List<ServiceNodeListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<ServiceNodeListEntity>>>> call, Response<BaseEntity<ListResponse<List<ServiceNodeListEntity>>>> response) {
                ShowLoading.postValue(2);
                if (response.body() == null || response.body().getData() == null)
                    return;
                if (response.body().getState().equals("ok")) {
                    serviceNodeList.postValue(response.body().getData().getList());
                } else {
                    toast.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<ServiceNodeListEntity>>>> call, Throwable t) {
                ShowLoading.postValue(2);
            }
        });
    }

}
